/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.contest.alarmclock;

import java.sql.Time;
import java.util.Calendar;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.contest.constant.Constant;
import android.contest.util.Log;

/**
 * The Alarms provider supplies info about Alarm Clock settings
 */
public class Alarms {
	
	/**
	 * Set alarm for reminding do laundry tonight
	 */
	public static void setWashingNotify(Context context){
		// default time, which can be changed according to the user preference.
		int day_advanced = 0;
		int hour = 20;
		int minute = 0;
		
        // if alarm is behind current time, do not set remind the user today
		Calendar ac = calculateAlarm(day_advanced, hour, minute);
        if (ac == null)
        	return;
        
        long atTimeInMillis = ac.getTimeInMillis();
        
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(Constant.ALARM_ALERT_ACTION);
        intent.putExtra(Constant.LAUNDRY_TYPE, Constant.WASH_CLOTHES);

        PendingIntent sender = PendingIntent.getBroadcast(
                context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (!Constant.TESTALARMDEBUGFLAG) {
            am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
            if (Log.LOGV) Log.v("** setWashingNotify atTime " + atTimeInMillis);
        } else {
            // a five-second alarm, for testing
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000,
                   sender);
            if (Log.LOGV) Log.v("** setWashingNotify 5 second later...");

        }
	}
	
	/** Set alarm for reminding dry the cloth and collect them back */
	public static void setDryingNotify(Context context){
		// default time, which can be changed according to the user perference.
		int day_advanced = 1;
		int hour = 7;
		int minute = 0;
		
        // if alarm is behind current time, do not set remind the user today
		Calendar ac = calculateAlarm(day_advanced, hour, minute);
        if (ac == null)
        	return;
        
        long atTimeInMillis = ac.getTimeInMillis();
        
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(Constant.ALARM_ALERT_ACTION);
        intent.putExtra(Constant.LAUNDRY_TYPE, Constant.DRY_CLOTHES);

        PendingIntent sender = PendingIntent.getBroadcast(
                context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (!Constant.TESTALARMDEBUGFLAG) {
            am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
            if (Log.LOGV) Log.v("** setDryingNotify atTime " + atTimeInMillis);
        } else {
            // a five-second alarm, for testing
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000,
                   sender);
            if (Log.LOGV) Log.v("** setDryingNotify 5 second later...");

        }
	}
	
	
	/** Set alarm for reminding dry the cloth and collect them back */
	public static void setCollectingNotify(Context context){
		// default time, which can be changed according to the user perference.
		int day_advanced = 1;
		int hour = 17;
		int minute = 0;
		
        // if alarm is behind current time, do not set remind the user today
		Calendar ac = calculateAlarm(day_advanced, hour, minute);
        if (ac == null)
        	return;
        
        long atTimeInMillis = ac.getTimeInMillis();
        
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(Constant.ALARM_ALERT_ACTION);
        intent.putExtra(Constant.LAUNDRY_TYPE, Constant.COLLECT_CLOTHES);
        PendingIntent sender = PendingIntent.getBroadcast(
                context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        if (!Constant.TESTALARMDEBUGFLAG) {
            am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender);
            if (Log.LOGV) Log.v("** setCollectingNotify atTime " + atTimeInMillis);
        } else {
            // a five-second alarm, for testing
            am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 5000,
                   sender);
            if (Log.LOGV) Log.v("** setCollectingNotify 10 second later...");

        }
	}
	
	public static void cancelLaundryAlarms(Context context){
		AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(Constant.ALARM_ALERT_ACTION); 
		PendingIntent pendingIntent = PendingIntent.getBroadcast(
				context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
		am.cancel(pendingIntent); 
		Log.d(Log.LOGALARM, "cancelLaundryAlarms in Alarms");
	}
	
	/**
     * Given an alarm in hours and minutes, return a time suitable for
     * setting in AlarmManager.
     * @param day Number of days advanced. 0, this day; 1 next day
     * @param hour Always in 24 hour 0-23
     * @param minute 0-59
     */
    public static Calendar calculateAlarm(int day_advanced, int hour, int minute) {

        // start with now
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());

        int nowHour = c.get(Calendar.HOUR_OF_DAY);
        int nowMinute = c.get(Calendar.MINUTE);

        // if alarm is behind current time, do not set remind the user today
        
        if(day_advanced <=0 ){
	        if (hour < nowHour  ||
	            hour == nowHour && minute <= nowMinute) {
	        	if(!Constant.TESTALARMDEBUGFLAG)
	        	return null;
	        }
        }
        
        c.add(Calendar.DATE, day_advanced);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        return c;
    }

    /**
     * If now is XX:XX:XX Oct 3th 2011, the return time is (0:0:0 + milli_t ms + day_dav days)
     * @param milli_t
     * @param day_adv
     * @return
     */
	public static Calendar calculateAlarm2(long milli_t, int day_adv) {
		// start with now
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(System.currentTimeMillis());
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.setTimeInMillis(c.getTimeInMillis() + milli_t);
        c.add(Calendar.DATE, day_adv);
        return c;
	}
}
